﻿/*global angular*/
/*jslint unparam:true*/
(function () {
    'use strict';
    var app = angular.module('app');
    app.factory('scheduleService', ['$http', '$q', function scheduleService($http, $q) {
        function getEvents(start, end, id) {
            var deferred = $q.defer();
            var e_date = end.getDate();
            var e_month = end.getMonth() + 1;
            var e_year = end.getFullYear();
            var e=e_date + "-" + e_month + "-" + e_year;
            var s_date = start.getDate();
            var s_month = start.getMonth() + 1;
            var s_year = start.getFullYear();
            var s = s_date + "-" + s_month + "-" + s_year;
            
            $http.get('../api/Scheduling/GetByDate', {
                params:
                    {
                        startParam: s,
                        endParam: e,
                        id:id
                    }
            }).
                success(function (data) {//, status, headers, config
                    deferred.resolve(data);
                }).
                error(function (data, status) {//, headers, config
                    deferred.reject(status);
                });
            return deferred.promise;
        }
        
        function getScheduleTypes() {
            var deferred = $q.defer();
            $http.get('../api/Classifiers/GetScheduleTypes').
                success(function (data) {//, status, headers, config
                    deferred.resolve(data);
                }).
                error(function (data, status) {//, headers, config
                    deferred.reject(status);
                });
            return deferred.promise;
        }

        function saveAppointment(id, personId) {
            var deferred = $q.defer();
            var value = { id: id, personId: personId };
            $http.post('../api/Scheduling', value ).
                success(function (data) {//, status, headers, config
                    deferred.resolve(data);
                }).
                error(function (data, status) {//, headers, config
                    deferred.reject(status);
                });
            return deferred.promise;
        }
        
        function clearAppointment(id, reason) {
            var deferred = $q.defer();
            $http({
                url: '../api/Scheduling/DeleteActivity',
                method: "POST",
                params: {
                    itemId: id,
                    reason: reason
                }
                //headers: headers
            }).
                success(function (d) {//, status, headers, config
                    deferred.resolve(d);
                }).
                error(function (d, status) {//, headers, config
                    deferred.reject(status);
                });
            return deferred.promise;
        }
        
        function getScheduleByPerson(personId) {
            var deferred = $q.defer();
            $http.get('../api/Scheduling/GetByPerson', {
                params:
                    {
                        personId: personId
                    }
            }).
                success(function (data) {//, status, headers, config
                    deferred.resolve(data);
                }).
                error(function (data, status) {//, headers, config
                    deferred.reject(status);
                });
            return deferred.promise;
        }

        // Define the functions and properties to reveal.
        var service = {
           // getDefinition: getDefinition,
            getEvents: getEvents,
            getScheduleTypes: getScheduleTypes,
            saveAppointment: saveAppointment,
            clearAppointment: clearAppointment,
            getScheduleByPerson: getScheduleByPerson
        };

        return service;
    }]);
}());